//
//  BRChoiceCityCell.m
//  carcareIOS
//
//  Created by 王然 on 16/8/4.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "BRChoiceCityCell.h"
#import "ChoiceCityCollectionCell.h"
#import "VioCityModel.h"
#define CELL_ID @"cellID"

@interface BRChoiceCityCell()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic , strong) NSMutableArray * choiceCitysList;

@property (nonatomic , strong)UICollectionView * collectionView;

@property (nonatomic , copy) void (^clickInde)(NSInteger index);

@end
@implementation BRChoiceCityCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createUI];
    }
    return self;
}
- (void)reloadChoiceCitys:(NSArray *)choiceCitys clickIndex:(void(^)(NSInteger index))indexClick
{
    if (indexClick) self.clickInde = indexClick;
    self.choiceCitysList = [NSMutableArray arrayWithArray:choiceCitys];
    
    [self.collectionView reloadData];
    if(self.choiceCitysList.count > 1)[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.choiceCitysList.count - 1 inSection:1] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
}
- (void)createUI
{
    UICollectionViewFlowLayout * collectionLayout = [[UICollectionViewFlowLayout alloc]init];
    collectionLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    collectionLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, CELL_HEIGHT) collectionViewLayout:collectionLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ChoiceCityCollectionCell class] forCellWithReuseIdentifier:CELL_ID];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.collectionView];
    
    
    
}
#pragma mark ---- UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.titleArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //self.titleArray[indexPath.section][indexPath.row]
    return (section == 0 ? 0 : self.choiceCitysList.count);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChoiceCityCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath];
    [cell reloadTitle:(indexPath.section == 0 ? @"" : ((VioCityModel *)self.choiceCitysList[indexPath.row]).name) subTitle:(indexPath.section == 0 ? @"" : @"") index:indexPath.row];
//    [cell reloadTitle:(indexPath.section == 0 ? self.titleArray[indexPath.section][indexPath.row] : ((VioCityModel *)self.choiceCitysList[indexPath.row]).name) subTitle:(indexPath.section == 0 ? @"车牌归属地" : @"") index:indexPath.row];
    cell.isHiddenCancleBtn = (indexPath.section == 0);
    __weak __typeof(self) wself = self;
    cell.cancleBtnClick = ^(NSInteger index)
    {
        if (wself.clickInde) wself.clickInde(index);
    };
    
    return cell;
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
{
    
}
#pragma mark ---- UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * titleStr = [NSString stringWithFormat:@"%@",(indexPath.section == 0 ? self.titleArray[indexPath.section][indexPath.row] : ((VioCityModel *)self.choiceCitysList[indexPath.row]).name)];
    CGSize size = [titleStr sizeWithFont:Label_Font maxWidth:200];
    return (CGSize){size.width + Label_X * 2 + Label_RIGHT + (indexPath.section == 0 ? 70 : 0), CELL_HEIGHT};
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(3, 5, 3, 5);
}
- (NSArray *)titleArray
{
    return @[@[@"上海"],@[@"其他选择地区列表",@"地区列表",@"其他选表"]];
}
+ (CGFloat)getCellHeight
{
    return CELL_HEIGHT;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
